﻿
var CONTEXT_PATH = "http://" + document.location.host;
var fillColor = "FFFFCC"; //鼠标上移颜色
var fillOpacity = 1;
var selectColor = "ffff00"; //选择颜色
var selectOpacity = 0.5;
var noneClass = "roomDiv-background-0"; //未预定的车位颜色
var submitClass = "roomDiv-background-1"; //自己预定的车位颜色
var othersClass = submitClass; //别人预定的车位颜色
var guideClass = "roomDiv-background-5"; //引导销控
var guideRClass = "roomDiv-background-5o5"; //引导销控
var orderClass = "roomDiv-background-4"; //后台预定
var redClass = "roomDiv-background-3"; //后台预定翻红
var redRClass = "roomDiv-background-3o5"; //后台预定翻红准备
var greenClass = "roomDiv-background-8"; //预定中的颜色

var roombackColor0 = "FFFFCC";
var roombackColor1 = "FF0000";
var roombackColor2 = "0066FF";
var roombackColor3 = "FF0099";
var roombackColor4 = "FFFF00";
var roombackColor5 = "FF6600";
var roombackColor6 = "fbb7b7";
var roombackColor7 = "9900CC";

var timeoutstamp = 5 * 60 * 1000;//购物车过期时间
var openloadindex;//layer加载层
var roomData = [];
var annarr = [];

//房间初始化
function RoomInit(initData) {
    var config = JSON.parse(initData.Config);//配置
    RoomConfig(config);
    var roomlist = JSON.parse(initData.RoomList);//房间数据
    var type = "snapped";
    if (window.InitType) {
        type = window.InitType;
    }
    ShowRoomBox(roomlist, 1, type);
    //var shoplist = JSON.parse(initData.ShopList);//购物车
    //if (typeof (window.SetRoomShopCart) === "function") {
    //    window.SetRoomShopCart(shoplist);
    //}
    closelayer();
//    var announcement = JSON.parse(initData.Announcement);//公告
//    RoomAnner(announcement);
}
//抢购结束
function timeend() {
    RoomSettlement();//房间结算
}

//房间公告
function RoomAnner(data) {
    annarr = [];
    for (var i in data) {
        if (data.hasOwnProperty(i)) {
            var ann = data[i];
            annarr.push(ann);
        }
    }
    SplaceAnner();
    if (annertime !== null) {
        clearInterval(annertime);
    }
    AnnerRun();
}

//生成公告
function SplaceAnner() {
    $("#ann .notice_active li").remove();
    for (var i in annarr) {
        if (annarr.hasOwnProperty(i)) {
            var ann = annarr[i];
            var li = $("<li class=\"notice_active_ch\"></li>");
            li.append("<span>" + ann.D_Title + "</span>");
            var date = ann.D_OperDate.substring(0, 11);
            li.append("<em>" + date + "</em>");
            $("#ann ul").append(li);
        }
    }
}

var annertime = null;
function AnnerRun() {
    var num = $('#ann .notice_active').find('li').length;
    if (num > 1) {
        annertime = setInterval('ShowAnner("#ann .notice_active")', 3500);
    }
}

function ShowAnner(obj) {
    $(obj)
        .find('ul')
        .animate({
                marginTop: "-3.6rem"
            },
            500,
            function() {
                $(this).css({ marginTop: "1px" }).find("li:first").appendTo(this);
            });

}

var longdowncount;//项目长时间倒计时时间
var shortdowncount;//项目短时间倒计时时间
//0,1、未开始，2、预览，3、抢购倒计时，4、抢购中，5、结束，6、结束，页面弹出
var timearr = [0, 0, 0, 0, 0, 0, 0];
function RoomConfig(option) {
    window.colcount = option.RoomLine;//房间每列显示行数
    timeoutstamp = option.ExpiredTime;//过期时间
    timearr[2] = option.PreviewTime;//预览时间戳
    timearr[3] = option.CountDownTime;//抢购倒计时
    timearr[4] = option.ShoppingTime;//抢购总时间戳
    window.cartmaxcount = option.ShoppingCartUpper * window.formcount;//购物车最大数量 * 客户筹数
    
    longdowncount = option.StateTime; //项目长时间倒计时时间
    var remainingtime = option.RemainingTime;//到下一步剩余时间
    var pagestate = parseInt(option.State); // 项目当前状态
    if (remainingtime !== 0) {
        //剩余时间不为0，修改下一步时间
        timearr[pagestate] = remainingtime;
    }
    if (typeof (window.ShowPageState) === "function") {
        window.ShowPageState(pagestate);
    }
    console.log(option.IsTest);
    if (option.IsTest == "1" && Checkfunction("Test")) {
        window.Test();
    }
}

function timeout() {
    $("#outdiv").remove();
    $(".cover_bg").hide();
}

//获取房间数据
function getroom(roomid) {
    return window.roomData[roomid];
}

//点击房间预约按钮
function submitRoomOrder(name, operation, cardno, tel, roomid) {
    var json = window.wsorder.roomseat(name, operation, cardno, tel, roomid);
    window.websocket.send(JSON.stringify(json));
}

//购物车数据提交
function UpdateShopCart(id, type, name, operation, cardno, tel) {
    var json = "";
    var cartid = window.GetRoomCartId(id);
    if (type === 1) {
        //房间购物车数据提交
        json = window.wsorder.roomcartsubmit(name, operation, cardno, tel, id, cartid);
    } else if (type === 2) {
        //房间购物车取消
        json = window.wsorder.roomunseat(name, operation, cardno, tel, id, cartid);
    }
    window.websocket.send(JSON.stringify(json));
}

//房间销控发送消息
function RoomGuideAjax(action, name, operation, id, tel, cardno, name2, clientid) {
    var json = eval("window.wsorder." +
        action +
        "('" +
        name +
        "','" +
        operation +
        "','" +
        id +
        "','" +
        tel +
        "','" +
        cardno +
        "','" +
        name2 +
        "','" +
        clientid +
        "')");
    window.websocket.send(JSON.stringify(json));
}

//房间预定
function SetRoomOrder(id, oper, shopid) {
    if (window.user === oper) {
        if (!window.ismonitor) {
            //购物车列表
            roomShopList.push({
                shopid: shopid,
                id: id,
                oper: oper,
                state: 1,
                time: timeoutstamp
            });
            closelayer();
            showRoomSeat();
            roomcartAnimate();
        }
    }
}

//房间批量更新
function RoomUpdateBatch(data) {
    for (var i in data) {
        if (data.hasOwnProperty(i)) {
            var room = data[i];
            var state = parseInt(room.toString().substr(0, 1));
            var id = parseInt(room.toString().substr(1));
            var rdata = {
                D_ID: id,
                D_state: state
            }
            RoomUpdate(rdata);
        }
    }
}

//房间批量更新收藏数
function RoomUpdateCollection(data) {
    for (var i in data) {
        if (data.hasOwnProperty(i)) {
            var room = data[i];
            var D_ID = room.D_ID;
            window.roomData[D_ID].D_CollectionCount = room.D_CollectionCount;
            window.roomData[D_ID].E_CollectionCount = room.E_CollectionCount;
            if (Checkfunction("UpdateCollectionCount")) {
                window.UpdateCollectionCount(D_ID, (room.D_CollectionCount + room.E_CollectionCount));
            }
        }
    }
}

//房间后台批量变更
function RoomBackUpdateBatch(data) {
    if (window.InitType && window.InitType === "collection") {
        RoomUpdateCollection(data);
        return;
    }
    for (var i in data) {
        if (data.hasOwnProperty(i)) {
            var room = data[i];
            var state = parseInt(room.D_state);
            var monitor = room.D_Monitor;
            var id = parseInt(room.D_ID);
            var rdata = {
                D_ID: id,
                D_state: state,
                D_Monitor: monitor,
                Name: room.Name,
                CardNo: room.CardNo,
                Tel: room.Tel
            }
            RoomUpdate(rdata);
        }
    }
}

//房间数据更新
function RoomUpdate(data) {
    if (window.roomloadend === undefined || !window.roomloadend) {
        return;
    }
    var id = data.D_ID;
    var state = data.D_state; //房间状态
    var monitor = data.D_Monitor === undefined ? 0 : parseInt(data.D_Monitor); //房间监控
    var name = data.Name === undefined ? "" : data.Name;
    var card = data.CardNo === undefined ? "" : data.CardNo;
    var tel = data.Tel === undefined ? "" : data.Tel;
    UpdateRoomDateState(id, state, monitor, name, card, tel); //更新房间数据
    
    var othercl = "";
    if (!window.ismonitor) {
        
            //非监控页面
            if (monitor === 4 || monitor === 5) {
                //引导销控准备,后台翻红预定准备
                othercl = greenClass;
            } else if (monitor === 1 || monitor === 3) {
                //引导销控，后台预定翻红
                othercl = othersClass;
            } else if (monitor === 2) {
                //客户页面，后台预定无反映
                othercl = noneClass;
            } else if (state === 1) {
                //预定中
                othercl = greenClass;
            } else if (state === 2) {
                //提交预定
                othercl = othersClass;
            } else if (state === 0) {
                othercl = noneClass;
            }
    } else {
        //监控页面
        if (monitor !== 0) {
            if (window.MonitorType === 1) {
                //房间监控页面
                if (monitor === 1) {
                    othercl = guideClass; //销控
                } else if (monitor === 4) {
                    othercl = guideRClass; //销控准备
                } else if (monitor === 2) {
                    othercl = orderClass; //后台预定
                } else if (monitor === 3) {
                    othercl = redClass; //后台预定翻红
                } else if (monitor === 5) {
                    othercl = redRClass; //后台预定翻红准备
                } else if (state === 0) {
                    othercl = noneClass;
                }
            } else if (window.MonitorType === 2) {
                //房间预留
                if (monitor === 2) {
                    othercl = orderClass; //后台预定
                } else if (monitor === 3) {
                    othercl = redClass; //后台预定翻红
                } else if (monitor === 5) {
                    othercl = redRClass; //后台预定翻红准备
                } else {
                    othercl = othersClass;
                }
            } else if (window.MonitorType === 3) {
                if (monitor === 4 || monitor === 5) {
                    //引导销控准备,后台翻红预定准备
                    othercl = greenClass;
                } else if (monitor === 1 || monitor === 3) {
                    //引导销控，后台预定翻红
                    othercl = othersClass;
                } else if (monitor === 2) {
                    //客户页面，后台预定无反映
                    othercl = noneClass;
                } else if (state === 1) {
                    //预定中
                    othercl = greenClass;
                } else if (state === 2) {
                    //提交预定
                    othercl = othersClass;
                } else if (state === 0) {
                    othercl = noneClass;
                }
            }
        } else if (state === 1) {
            //预定中
            othercl = greenClass;
        } else if (state === 2) {
            //提交预定
            othercl = othersClass;
        } else if (state === 0) {
            othercl = noneClass;
        }
    }
    highlightSelectRoom($("#" + id), othercl);
}

//房间预定双击验证
function RoomMonitorClickValidated(id) {
    var room = getroom(parseInt(id));
    var monitor = room.D_Monitor;
    //后台预定跟预定翻红准备双击才有反映
    if (monitor === 5 || monitor === 2) {
        return true;
    }
    return false;
}

//房间引导销控双击验证
function RoomGuideClickValidated(id) {
    var room = getroom(parseInt(id));
    var monitor = room.D_Monitor;
    //引导销控准备状态双击才有反映
    if (monitor === 4) {
        return true;
    }
    return false;
}

//取消房间预约
function SetRoomUnOrder(id) {
    //更新购物车数据并显示
    SetRoomCartState(id, 2);
    //清空倒计时
    CleanCountStop();
    RoomSettlement();//房间结算
}

//更新房间状态
function UpdateRoomDateState(id, state, monitor, name, card, tel) {
    window.roomData[id].D_state = state;
    window.roomData[id].D_Monitor = monitor;
    window.roomData[id].Name = name;
    window.roomData[id].CardNo = card;
    window.roomData[id].Tel = tel;
}
//更新房间购买人信息
function UpdateRoomDataName(id, name, tel, cardno, csrial) {
    window.roomData[id].Name = name;
    window.roomData[id].Tel = tel;
    window.roomData[id].CardNo = cardno;
    window.roomData[id].D_SerialNumber = csrial;
}

//高亮被已经预定的房间
function highlightOrderRoom(id, operation, state, monitor) {
    var cssclass;
    operation = $.trim(operation);
    if (monitor === 4 || monitor === 5) {
        cssclass = greenClass;
    } else if (monitor === 2) {
        cssclass = noneClass;
    } else if (monitor !== 0) {
        cssclass = submitClass;
    } else if (state === 0) {
        cssclass = noneClass;
    } else if (operation === window.user) {
        if (state === 1) {
            cssclass = greenClass;
        } else {
            cssclass = submitClass;
        }
    } else if (state === 1) {
        cssclass = greenClass;
    } else {
        cssclass = submitClass;
    }
    return cssclass;
}

//高亮被引导预定的车位 房间预留界面
function highlightOrderRoomMon(id, operation, state, monitor) {
    var cssclass;
    operation = $.trim(operation);
    if (monitor === 1 || monitor === 4) {
        //引导销控
        cssclass = submitClass;
    } else if (monitor === 2) {
        //后台预定
        cssclass = orderClass;
    } else if (monitor === 3) {
        //后台预定翻红
        cssclass = redClass;
    } else if (monitor === 5) {
        //后台预定翻红
        cssclass = redRClass;
    } else if (state === 0) {
        cssclass = noneClass;
    } else if (state === 1) {
        cssclass = greenClass;
    } else if (operation === window.user) {
        cssclass = submitClass;
    } else {
        cssclass = submitClass;
    }
    return cssclass;
}

//高亮被引导预定的车位 房间监控界面
function highlightOrderRoomGui(id, operation, state, monitor) {
    var cssclass;
    operation = $.trim(operation);
    if (monitor === 1) {
        //引导销控
        cssclass = guideClass;
    } else if (monitor === 4) {
        //引导销控准备
        cssclass = guideRClass;
    } else if (monitor === 2) {
        //后台预定
        cssclass = orderClass;
    } else if (monitor === 3) {
        //后台预定翻红
        cssclass = redClass;
    } else if (monitor === 5) {
        //后台预定翻红准备
        cssclass = redRClass;
    } else if (state === 0) {
        cssclass = noneClass;
    } else if (state === 1) {
        cssclass = greenClass;
    } else if (operation === window.user) {
        cssclass = submitClass;
    } else {
        cssclass = submitClass;
    }
    return cssclass;
}

//高亮被引导预定的车位 房间监控界面
function highlightOrderRoomGuiNew(id, operation, state, monitor) {
    var cssclass;
    operation = $.trim(operation);
    if (monitor === 1) {
        //引导销控
        cssclass = submitClass;
    } else if (monitor === 4) {
        //引导销控准备
        cssclass = greenClass;
    } else if (monitor === 2) {
        //后台预定
        cssclass = noneClass;
    } else if (monitor === 3) {
        //后台预定翻红
        cssclass = submitClass;
    } else if (monitor === 5) {
        //后台预定翻红准备
        cssclass = greenClass;
    } else if (state === 0) {
        cssclass = noneClass;
    } else if (state === 1) {
        cssclass = greenClass;
    } else if (operation === window.user) {
        cssclass = submitClass;
    } else {
        cssclass = submitClass;
    }
    return cssclass;
}

//房间协议颜色
function highlightPactRoom(orderstate, state) {
    var cssclass;
    if (orderstate === 1) {
        //已签
        cssclass = submitClass;
    } else {
        if (state === 2) {
            cssclass = guideClass;
        } else {
            cssclass = noneClass;
        }
    }
    return cssclass;
}

function highlightAsCollection(collection) {
    var cssclass;
    if (collection <= 0) {
        cssclass = noneClass;
    }
    else if (collection <= 10) {
        cssclass = "collection" + collection;
    } else if (collection > 10) {
        cssclass = "collection10";
    }
    return cssclass;
}

//高亮预定的车位
function highlightSelectRoom(obj, classcolor) {
    var cla = obj.attr("class");
    obj.attr("class", removeClass(cla));
    obj.addClass(classcolor);
}

function removeClass(css) {
    if (/roomDiv-background/g.test(css)) {
        css = css.replace(/[\s]roomDiv-background-\w*[\s]?/g, " ");
    }
    return css;
}

//清除房间选中状态
function cleanHighlightRoomColor(obj) {
    obj.css({ "background-color": "#FFFFCC", "opacity": window.fillOpacity });
}

//关闭预约窗口
var closeRoomOrderWin = function () {
    closelayer();
    $(".info_div").hide();
    $(".cover_bg").hide();
};

function closelayer() {
    $(".roomChecked").removeClass("roomChecked");
    if (window.layerindex) {
        window.layer.close(window.layerindex);
    }
    if (window.configload) {
        window.layer.close(window.configload);
    }
}

//显示剩余机会提示
function ShowTip(count) {
    if (count <= 0) {
        count = 0;
    } 
    $("#lastcount").html(count);
}

//房间提交和过期
function UpdateRoomSeat(id, state, name, tel, cardno, room) {
    SetRoomCartState(id, state);
    //清空倒计时
    CleanCountStop();
    BtnHide();
    if (state === 3) {
        //房源确定提交
        UpdateRoomDataName(id, name, tel, cardno, room.D_SerialNumber);
    }
    else if (state === 4) {
    }

    RoomSettlement();//房间结算
}

//判断是否有房源确定次数
function CheckSubmitCount() {
    var submitcount = GetOrderRoomCount();//客户认筹数
    return submitcount >= window.formcount;
}

//判断是否还有选房次数,返回值 true: 无次数， false: 还有次数
function CheckSelectCount() {
    var currentcount = roomShopList.length;//客户预定房源次数(包含取消)
    var maxcount = window.cartmaxcount;//客户可预定次数（基本次数*认筹数）
    return currentcount >= maxcount;
}

//房间结算，显示是否有确定的房源
function RoomSettlement() {
    if (window.GetRoomCartStateInOrder() === 0) {
        //房源提交次数用尽直接结算
        if (CheckSubmitCount()) {
            successbuy();
        } else {
            if (CheckSelectCount()) {
                //无次数根据是否有确定房源结算
                var scount = GetOrderRoomCount();
                if (scount > 0) {
                    successbuy();
                } else {
                    stopbuy();
                }
            }
        }
    }
}

//成功购买页面
function successbuy(end) {
    ShowTip(0);
    $("#enddiv").remove();
    $("#successdiv").remove();
    window.layer.closeAll();
    var div = $("<div id='enddiv' style='display:none; border-radius: 100px;'></div>");

    if (end) {
        div.append("<div style='font:bold 36px/40px 宋体; height:50px;'>本次抢购已经结束！</div>");
    }
    var success = $("<div style='text-align:center; padding:20px;' id='successdiv'></div>");
    var roomcart = $("<div style='font:bold 50px/50px 宋体; color: #ff000a;'></div>");
    var str = "";
    var searials = "订单号：";
    var roomid = 0;
    $(".cartcardsuc").each(function () {
        var id = $(this).attr("tid");
        roomid = id;
        var room = getroom(id);
        str += room["D_BanName"] + " " + room["D_RoomNo"] + "号房";
        searials += room["D_SerialNumber"] + "    ";
        str += "<br />";
    });
    roomcart.html(str);

    success.append("<div style='font:bold 14px/40px 宋体; text-align:left;' id='D_SerialNumber'>" + searials+"</div>");
    success.append("<div style='font:bold 36px/40px 宋体;'>恭喜您成功认购【悦丽花园】</div>");
    success.append(roomcart);
    success.append("<div style='font:bold 26px/40px 宋体;'>认购人:" +
        $("#name").val() +
        "  手机:" +
        $("#tel").val() +
        "</div>");
    success.append("<div style='font:bold 26px/40px 宋体;'>身份证:" + $("#cardno").val() + "</div>");
    success.append("<div style='font:bold 24px/40px 宋体;'>vip热线:88866888</div>");
    if (window.IsTest && window.IsTest == 1) {
        success.append("<div style='font:bold 16px/40px 宋体;color: green'>公测不能作为正式凭证，请在2017-07-30 09:00准时参加正式活动</div>");
    }
    var divbutton = $("<div class=\"cart-button\" style=\"text-align:center; height: 60px;\"></div>");
    var button1 = $("<div class=\"layui-btn layui-btn-radius layui-btn-normal\" onclick=\"canvassss()\" >保存</div>");
    var button2 = $("<div class=\"layui-btn layui-btn-radius layui-btn-normal\" onclick=\"closelayer()\" >关闭</div>");
    var button3 = $("<div class=\"layui-btn layui-btn-radius layui-btn-normal\" onclick=\"loginout()\">退出</div>");
    var button4 = $("<div class=\"layui-btn layui-btn-radius layui-btn-normal\" id='smsbtn' style=\"display:none;\" onclick=\"SmsResend(" + roomid+")\">认购短信重发</div>");
    var button5 = $("<div class=\"layui-btn layui-btn-radius layui-btn-disabled\" id='count60' onclick=\"settime60()\">认购短信重发(180秒)</div>");
//    divbutton.append(button1);
    divbutton.append(button2);
    divbutton.append(button3);
    divbutton.append(button4);
    divbutton.append(button5);
    settime60();
    div.append(success);
    div.append(divbutton);
    $("body").append(div);
    window.layerindex = window.layer.open({
        type: 1,
        title: false,
        closeBtn: false,
        shadeClose: false,
        area: ["600px"],
        skin: "layui-layer-lan",
        id: "end",
        move: false,
        content: $("#enddiv")
    });
}
var countdown60 = 180;
function settime60() {
    var obj = $("#count60");
    if (countdown60 === 0) {
        $("#smsbtn").show();
        $("#count60").hide();
        obj.html("认购短信重发(180秒)");
        countdown60 = 180;
        return;
    } else {
        $("#smsbtn").hide();
        obj.html("认购短信重发(" + countdown60 + "秒)");
        countdown60--;
    }
    setTimeout(function () {
            settime60();
        },
        1000);
}  

function SmsResend(roomid) {
    $("#smsbtn").hide();
    $("#count60").show();
    settime60();
    var user = window.user;
    var json = window.wsorder.smsresend(roomid, user);
    window.websocket.send(JSON.stringify(json));
}

function canvassss() {
    //打印
    html2canvas($("#successdiv"),
        {
            onrendered: function (canvas) {
                var btn = $("<a></a>");
                btn.attr('href', canvas.toDataURL());
                btn.attr('download', '房源单.jpg');
                btn.appendTo("body");
                btn[0].click();
                btn.remove();
            },
            allowTaint: true,
            background: "#fff"
        });
}

//购买结束
function stopbuy() {
        $("#stopdiv").remove();
        window.layer.open({
            type: 1,
            title: false,
            shadeClose: false,
            time: 100000,
            id: "end",
            move: false,
            skin: "layer-skin-raduis",
            area: "600px",
            content: "<div style='font:bold 36px/50px 宋体; text-align:center; background: #ff0000; color: #fff;'>很遗憾，您没在规定次数内购房！</div>"
        });
}


var loginout = function () {
    $.ajax({
        type: 'post', dataType: 'json',
        url: '../data/Tuser.ashx',
        data: { Action: 'logout' },
        success: function (result) {
            javascript: location.replace("../../rules.aspx");
            alert("您已退出登录");
        }
    });
}
